<?php

require_once 'Framework/Controller/Action.php';
require_once 'application/models/User/Admin.php';

class IndexController extends Framework_Controller_Action
{
    public function init()
    {
    //	$helper = $this->_helper->init;
	//	$helper->out();
		$this->view->current_page="index";
		$this->_admin = new User_Admin();
		$this->view->adminnum = $this->session->get('adminnum');
    }
    
    public function indexAction()
    {
		$login = $this->session->get('name');
		$pass = $this->session->get('pass');
		
		// no post, no cookies, sholud authorize
        if ($login=='' || $pass=='') {
			 $this->_redirect('/admin/index/authorize/');
		}
		
		if (!$id = $this->_admin->getIsValidAdmin($login,$pass)) {
			$this->view->message = 'Invalid Login or password';
			$this->_redirect('/admin/index/authorize/');
		}
		$this->view->priv = $this->_admin->getPossibilities($id);
		$this->view->group = $this->_admin->getAdminGroup($id);
		$this->view->username = $login;
		$this->_admin->setLogin($id);
        $this->render();
    }
    
	public function authorizeAction()
	{
		$login = $this->getRequest()->name;
		$pass = $this->getRequest()->pass;
		if ($login!='' && $pass!='') {
			if (!$this->_admin->getIsValidAdmin($login,$pass)) {
				$this->view->message = 'Invalid Login or password';
			} else {
				$this->session->set('name',$login);
				$this->session->set('pass',$pass);
				$this->session->set('authorize',true);
				$id = User_Admin::findAdminIdByLogin($login);
				$adminnum = User_Admin::getAdminGroupId($id);
				$adminnum = (isset($adminnum['groupId']))?$adminnum['groupId'] : 0;
				$this->session->set('adminnum',$adminnum);
				$this->_redirect('/admin/index/');
			}
		}
		$this->render();
	}
	
	public function logoutAction()
    {
        $this->session->destroy(true);   
        $this->_redirect('/admin/');
    }
}
